package com.slkj.controller;

import com.alibaba.fastjson.JSONObject;
import com.slkj.dto.Result;
import com.slkj.dto.StatusCode;
import com.slkj.dto.UserRouteDTOList;

import com.slkj.entity.UserRoute;
import com.slkj.service.RouteService;

import com.slkj.util.RequestResponseBox;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author admin
 * @version 1.0.0
 * @ClassName RouteController.java
 * @Description TODO
 * @createTime 2020年03月12日 15:27:00
 */
@Transactional
@RestController
@RequestMapping("${address.prefix}/route")
@CrossOrigin
public class RouteController {

    @Autowired
    private RouteService routeService;

    private Logger log = LoggerFactory.getLogger(RouteController.class);

    @PostMapping("/getRoute")
    public Result getRoute() {
        log.info("----------------------->>>getRoute(@RequestBody String tokenJson)");

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }

        UserRouteDTOList route = routeService.getRoute(username);

        if (route == null) {
            log.error("查询角色,无数据");
            return new Result(true, StatusCode.NULL, "无数据");
        }

        log.info("查询角色组合成功,返回View");
        return new Result(true, StatusCode.OK, "查询角色组合成功", route);
    }

    @PostMapping("/getBtn")
    public Result getBtn() {
        log.info("----------------------->>>getBtn");

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        List<UserRoute> userRouteList = routeService.getBtn();

        log.info("查询权限和按钮成功,返回View");
        return new Result(true, StatusCode.OK, "查询权限和按钮成功", userRouteList);
    }

    @PostMapping("/getBtnList")
    public Result getBtnList(@RequestBody String data) {
        log.info("----------------------->>>getBtnList");
        JSONObject jsonObject = JSONObject.parseObject(data);
        String roleId = jsonObject.getString("roleId");
        if (StringUtils.isBlank(roleId) || !StringUtils.isNumeric(roleId)) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        List<String> btnList = routeService.getBtnList(roleId);

        log.info("查询权限和按钮成功,返回View");
        return new Result(true, StatusCode.OK, "查询权限和按钮成功", btnList);
    }

    @PostMapping("/updateRoute")
    public Result updateRoute(@RequestBody String data) {
        log.info("----------------------->>>updateRoute");
        JSONObject jsonObject = JSONObject.parseObject(data);
        String roleId = jsonObject.getString("roleId");
        List<String> btnList = JSONObject.parseArray(jsonObject.getString("btnList"), String.class);
        if (StringUtils.isBlank(roleId) || !StringUtils.isNumeric(roleId) || btnList == null) {
            return new Result(false, StatusCode.ERROR, "参数异常");
        }

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");


        if (StringUtils.isBlank(username) || username.equals("null")) {
            return new Result(false, StatusCode.LOGINDISABLED, "登录失效,请重新登录");
        }
        routeService.updateRoute(roleId, btnList);

        return new Result(true, StatusCode.OK, "success");
    }


}
